package HP.com.book.wangyu.dao.impl;

import HP.com.book.wangyu.dao.BookDao;
import HP.com.book.wangyu.pojo.Book;
import HP.com.book.wangyu.util.PrintUI;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author wy
 */
public class BookDaoImpl implements BookDao {

    @Override //查询图书信息
    public Book selectBookInfo(String bookName) {
        Book bookInfo = new Book();
        try {
            String sql = "select * from BM_BOOK where bookname= ?";
            PreparedStatement pre = this.getConn().prepareStatement(sql);
            pre.setString(1, "JAVA");
            ResultSet res = pre.executeQuery();
            while (res.next()) {
                bookInfo.setBookId(res.getInt("bookid"));
                bookInfo.setBookName(res.getString("bookname"));
                bookInfo.setBookAuthor(res.getString("bookautor"));
                bookInfo.setBookCategory(res.getString("booktype"));
                bookInfo.setBookprice(res.getDouble("bookprice"));
            }
            res.close();
            pre.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bookInfo;
    }

    @Override //查询所有图书
    public void selectAllbook() {
        try {
            String sql = "select * from BM_BOOK";
            PreparedStatement pre = this.getConn().prepareStatement(sql);
            ResultSet res = pre.executeQuery();
            PrintUI.printListStart();
            while (res.next()) {
                System.out.print(res.getInt("bookid") + "\t\t");
                System.out.print(res.getString("bookname") + "\t\t");
                System.out.print(res.getString("bookautor") + "\t\t");
                System.out.print(res.getString("booktype") + "\t\t");
                System.out.println("￥" + res.getDouble("bookprice") + "\t\t");
            }
            res.close();
            pre.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override //修改图书信息
    public void updateBookInfo() {
        PrintUI.printBookUpdate(0);
        String oldBookName = new UserDaoImpl().inPut();
        PrintUI.printBookUpdate(1);
        PrintUI.printListStart();
        Book book = this.selectBookInfo(oldBookName);
        System.out.println(book.getBookId() + "\t\t" + book.getBookName() + "\t\t" + book.getBookAuthor() + "\t\t" + book.getBookCategory() + "\t\t" + book.getBookprice());
        PrintUI.printBookUpdate(2);
        int bookId = new Integer(new UserDaoImpl().inPut());
        PrintUI.printBookUpdate(3);
        String bookName = new UserDaoImpl().inPut();
        PrintUI.printBookUpdate(4);
        String bookAthor = new UserDaoImpl().inPut();
        PrintUI.printBookUpdate(5);
        String bookType = new UserDaoImpl().inPut();
        PrintUI.printBookUpdate(6);
        double bookPrice = Double.valueOf(new UserDaoImpl().inPut());
        try {
            String sql = "update BM_BOOK set BOOKID=?,BOOKNAME=?,BOOKAUTOR=?,BOOKTYPE=?,BOOKPRICE=? where BOOKNAME=?";
            PreparedStatement ps = this.getConn().prepareStatement(sql);
            ps.setInt(1, bookId);
            ps.setString(2, bookName);
            ps.setString(3, bookAthor);
            ps.setString(4, bookType);
            ps.setDouble(5, bookPrice);
            ps.setString(6, oldBookName);
            ps.executeUpdate();
            System.out.println("修改成功!");
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 图书添加
     */
    @Override //添加图书
    public void addBook() {
        PrintUI.printBookAdd(0);
        PrintUI.printBookAdd(1);
        int bookId = new Integer(new UserDaoImpl().inPut());
        PrintUI.printBookAdd(2);
        String bookName = new UserDaoImpl().inPut();
        PrintUI.printBookAdd(3);
        String bookAthor = new UserDaoImpl().inPut();
        PrintUI.printBookAdd(4);
        String bookType = new UserDaoImpl().inPut();
        PrintUI.printBookAdd(5);
        double bookPrice = Double.valueOf(new UserDaoImpl().inPut());
        try {
            String sql = "insert into BM_BOOK values(?,?,?,?,?)";
            PreparedStatement ps = this.getConn().prepareStatement(sql);
            ps.setInt(1, bookId);
            ps.setString(2, bookName);
            ps.setString(3, bookAthor);
            ps.setString(4, bookType);
            ps.setDouble(5, bookPrice);
            ps.executeUpdate();
            System.out.println("添加成功!");
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override //删除图书
    public void delectBook() {
        PrintUI.printBookDelect(1);
        String name = new UserDaoImpl().inPut();
        PrintUI.printBookDelect(2);
        String confirm = new UserDaoImpl().inPut();
        if (confirm.equals("y")) {
            try {
                String sql = "DELETE from BM_BOOK where bookname = ?";
                PreparedStatement ps = this.getConn().prepareStatement(sql);
                ps.setString(1, name);
                ps.executeUpdate();
                System.out.println("删除成功!");
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override //获取Connection
    public Connection getConn() {

        try {
            String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
            String URL = "jdbc:oracle:thin:localhost:1521:orcl";
            String UNAME = "system";
            String UPWD = "123456";
            Class.forName(DRIVER_CLASS);
            Connection conn = DriverManager.getConnection(URL, UNAME, UPWD);
            return conn;
        } catch (Exception e) {
            e.getMessage();
        }
        return null;
    }
}

